/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.webservicecomponent.metadata.serializer.obsolete.serializer;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.inspur.edp.caf.cef.schema.base.element.serializer.ParameterDeSerializer;
import com.inspur.edp.caf.cef.schema.base.element.serializer.ParameterSerializer;
import com.inspur.edp.caf.cef.schema.base.element.serializer.template.ParameterSerializerTemplate;
import com.inspur.edp.caf.cef.schema.base.serializer.SerializeContext;
import com.inspur.edp.caf.cef.schema.base.utils.JsonUtil;
import com.inspur.edp.caf.cef.schema.element.Parameter;
import com.inspur.edp.webservicecomponent.metadata.parameter.HelpFetchValueSetting;
import com.inspur.edp.webservicecomponent.metadata.parameter.ParameterFetchValueMode;
import com.inspur.edp.webservicecomponent.metadata.serializer.obsolete.WSParameter;

public class WSParameterSerializer implements ParameterSerializer, ParameterDeSerializer {
    @Override
    public Parameter deSerialize(JsonNode jObj, SerializeContext context) {
        WSParameter wsParameter = new WSParameter();
        ParameterSerializerTemplate tempSerializer = new ParameterSerializerTemplate();
        tempSerializer.deserialize(wsParameter, jObj, context);
        wsParameter.setKind(WSParameter.KIND);
        ParameterFetchValueMode fetchValueMode = ParameterFetchValueMode.NONE;
        String fetchValueModeStr = JsonUtil.getPropValue(jObj, "fetchValueMode", String.class);
        if (fetchValueModeStr != null) {
            if (fetchValueModeStr.equals("const")) {
                fetchValueMode = ParameterFetchValueMode.CONST;
            } else if (fetchValueModeStr.equals("enum")) {
                fetchValueMode = ParameterFetchValueMode.ENUM;
            } else if (fetchValueModeStr.equals("express")) {
                fetchValueMode = ParameterFetchValueMode.EXPRESS;
            } else if (fetchValueModeStr.equals("help")) {
                fetchValueMode = ParameterFetchValueMode.HELP;
                ObjectNode fetchValueSettingJObj = JsonUtil.getPropValue(jObj, "fetchValueSetting", ObjectNode.class);
                if (fetchValueSettingJObj != null) {
                    HelpFetchValueSetting helpSetting = new HelpFetchValueSetting();
                    helpSetting.setHelpId(JsonUtil.getPropValue(fetchValueSettingJObj, "helpId", String.class));
                    helpSetting.setHelpCode(JsonUtil.getPropValue(fetchValueSettingJObj, "helpCode", String.class));
                    helpSetting.setHelpName(JsonUtil.getPropValue(fetchValueSettingJObj, "helpName", String.class));
                    helpSetting.setServiceUnit(JsonUtil.getPropValue(fetchValueSettingJObj, "serviceUnit", String.class));
                    wsParameter.setFetchValueModeSetting(helpSetting);
                }
            } else if (fetchValueModeStr.equals("none")) {
                fetchValueMode = ParameterFetchValueMode.NONE;
            }
        }
        wsParameter.setFetchValueMode(fetchValueMode);
        return wsParameter;
    }

    @Override
    public JsonNode serialize(Parameter type, SerializeContext context) {
        ObjectNode parameterJObj = new ObjectMapper().createObjectNode();
        ParameterSerializerTemplate tempSerializer = new ParameterSerializerTemplate();
        tempSerializer.serialize(parameterJObj, (WSParameter) type, context);
        WSParameter wsParameter = (WSParameter) type;
        if (wsParameter.getFetchValueMode() == ParameterFetchValueMode.CONST) {
            JsonUtil.setPropValue(parameterJObj, "fetchValueMode", "const");
        } else if (wsParameter.getFetchValueMode() == ParameterFetchValueMode.ENUM) {
            JsonUtil.setPropValue(parameterJObj, "fetchValueMode", "enum");
        } else if (wsParameter.getFetchValueMode() == ParameterFetchValueMode.EXPRESS) {
            JsonUtil.setPropValue(parameterJObj, "fetchValueMode", "express");
        } else if (wsParameter.getFetchValueMode() == ParameterFetchValueMode.HELP) {
            JsonUtil.setPropValue(parameterJObj, "fetchValueMode", "help");
            if (wsParameter.getFetchValueModeSetting() != null) {
                HelpFetchValueSetting helpSetting = (HelpFetchValueSetting) wsParameter.getFetchValueModeSetting();
                ObjectNode helpSettingJObj = new ObjectMapper().createObjectNode();
                JsonUtil.setPropValue(helpSettingJObj, "helpId", helpSetting.getHelpId());
                JsonUtil.setPropValue(helpSettingJObj, "helpCode", helpSetting.getHelpCode());
                JsonUtil.setPropValue(helpSettingJObj, "helpName", helpSetting.getHelpName());
                JsonUtil.setPropValue(helpSettingJObj, "serviceUnit", helpSetting.getServiceUnit());
                JsonUtil.setPropValue(helpSettingJObj, "kind", helpSetting.getKind());
                JsonUtil.setPropValue(parameterJObj, "fetchValueSetting", helpSettingJObj);
            }
        } else if (wsParameter.getFetchValueMode() == ParameterFetchValueMode.NONE) {
            JsonUtil.setPropValue(parameterJObj, "fetchValueMode", "none");
        }

        return parameterJObj;
    }
}
